package com.kvest.service_test.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import com.kvest.service_test.R;
import com.kvest.service_test.ServiceTestActivity;

import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * User: Kvest
 * Date: 15.03.13
 * Time: 22:57
 * To change this template use File | Settings | File Templates.
 */
public class TestService extends Service {
    private static final String LOG_TAG = "TestService";
    private static final String COMMAND_ID = "command_id";
    private static final String COMMAND_PARAM = "command_param";

    @Override
    public IBinder onBind (Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(LOG_TAG, "onCreate");

        notifyCreated();
    }

    @Override
    public void onDestroy () {
        super.onDestroy();
        Log.d(LOG_TAG, "onDestroy");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(LOG_TAG, "onStartCommand");

        int commandId = intent.getIntExtra(COMMAND_ID, -1);
        String param = intent.getStringExtra(COMMAND_PARAM);
        switch (commandId) {
            case 0 : firstAction(param, startId); break;
            case 1 : secondAction(param, startId); break;
            case 2 : thirdAction(param, startId); break;
        }

        return super.onStartCommand(intent, flags, startId);
    }

    private void firstAction(final String param, final int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(LOG_TAG, "start firstAction with param=" + param);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch(InterruptedException interruptedException) {

                }
                Log.d(LOG_TAG, "stop firstAction");
                stopSelf(startId);
            }
        }).start();
    }

    private void secondAction(final String param, final int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(LOG_TAG, "start secondAction with param=" + param);
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch(InterruptedException interruptedException) {

                }
                Log.d(LOG_TAG, "stop secondAction");
                stopSelf(startId);
            }
        }).start();
    }

    private void thirdAction(final String param, final int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(LOG_TAG, "start thirdAction with param=" + param);
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch(InterruptedException interruptedException) {

                }
                Log.d(LOG_TAG, "stop thirdAction");
                stopSelf(startId);
            }
        }).start();
    }

    private void notifyCreated() {
        Notification notification = new Notification(R.drawable.icon, "Service started", System.currentTimeMillis());

        Intent intent = new Intent(this, ServiceTestActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

        notification.setLatestEventInfo(this, "Service test", "Service started", pIntent);

        // ставим флаг, чтобы уведомление пропало после нажатия
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // отправляем
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(1, notification);
    }
}
